﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using STDTO.Interfaces;
using System.Runtime.Serialization;
using STDTO.DataTransferObjects.Impl;

namespace STDTO.DataTransferObjects.Base
{
    [DataContract]
    public abstract class UnitySelfTrackingDataTransferObjectBase<T> : SelfTrackingDataTransferObjectBase<T> 
    {
        protected  IUnityContainer _unityContainer;

        public UnitySelfTrackingDataTransferObjectBase(IUnityContainer unityContainer)
        {
            _unityContainer = unityContainer;
            InnerUnitySingleton.Instance.Init(unityContainer);
        }

        protected override T GetServerSideInstance()
        {
            return _unityContainer.Resolve<T>();
        }

        protected override IDtoInstanceCreator GetDtoInstanceCreator()
        {
            return _unityContainer.Resolve<IDtoInstanceCreator>();
        }

        [OnDeserialized]
        protected void ResetUnity(StreamingContext context)
        {
            _unityContainer = InnerUnitySingleton.Instance.UnityContainer;
        }
    }
}
